#include <esp_adc_cal.h>
#include <driver/adc.h>
// 定义 MPPT 相关的参数
#define DEFAULT_VREF_1 1100        // 默认1.1V的参考电压
#define DEFAULT_VREF_3 3300        // 默认1.1V的参考电压
#define NO_OF_SAMPLES 128          // ADC采样次数
#define ADC_WIDTH ADC_WIDTH_12Bit  // ADC 12位宽度
#define ADC_ATTEN_1 ADC_ATTEN_0db  // 0dB衰减器// 默认1.1V的参考电压
#define ADC_ATTEN_3 ADC_ATTEN_11db // 11dB衰减器// 默认3.3V的参考电压
#define xdc_U_PIN ADC1_CHANNEL_7
#define tyn_I_PIN ADC1_CHANNEL_6 // 连接太阳能电池板电流的模拟输入引脚
#define MOS_PIN ADC1_CHANNEL_5
#define tyn_U_PIN ADC1_CHANNEL_4 // 连接太阳能电池板电压的模拟输入引脚

esp_adc_cal_characteristics_t *adc_chars_1;
esp_adc_cal_characteristics_t *adc_chars_3;
int adc = 0;

void chushihuaADC()
{
    adc1_config_width(ADC_WIDTH);                      // 设置ADC为12位宽度
    adc1_config_channel_atten(tyn_U_PIN, ADC_ATTEN_1); // 配置ADC通道为6dB衰减器
    adc1_config_channel_atten(xdc_U_PIN, ADC_ATTEN_1); // 配置ADC通道为6dB衰减器
    adc1_config_channel_atten(MOS_PIN, ADC_ATTEN_3);   // 配置ADC通道为6dB衰减器
    adc1_config_channel_atten(tyn_I_PIN, ADC_ATTEN_3); // 配置ADC通道为6dB衰减器
    // 使用eFuse校准ADC，并获取校准值
    adc_chars_1 = (esp_adc_cal_characteristics_t *)malloc(sizeof(esp_adc_cal_characteristics_t));
    esp_adc_cal_value_t val_type_1 = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_1, ADC_WIDTH, DEFAULT_VREF_1, adc_chars_1);
    adc_chars_3 = (esp_adc_cal_characteristics_t *)malloc(sizeof(esp_adc_cal_characteristics_t));
    esp_adc_cal_value_t val_type_3 = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_3, ADC_WIDTH, DEFAULT_VREF_3, adc_chars_3);
}
void tyn_u()
{
    static int adc_reading = 0;
    adc_reading += adc1_get_raw((adc1_channel_t)tyn_U_PIN);
    // adc_reading += analogReadMilliVolts(32);
    if (adc >= NO_OF_SAMPLES)
    {
        adc_reading /= NO_OF_SAMPLES;
        adc_reading = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars_1);
        tyn_U = ((R1 + R2) / R2 * adc_reading);
        adc_reading = 0;
    }
}

void tyn_i()
{
    static int adc_reading = 0;
    // adc_reading += adc1_get_raw((adc1_channel_t)tyn_I_PIN);
    adc_reading += analogReadMilliVolts(34);

    if (adc >= NO_OF_SAMPLES)
    {
        adc_reading /= NO_OF_SAMPLES;
        adc_reading = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars_3);
        tyn_I = map(adc_reading, 142, 3139, 2000, 64061);
        adc_reading = 0;
    }
}

void xdc_u()
{
    static int adc_reading = 0;
    adc_reading += adc1_get_raw((adc1_channel_t)xdc_U_PIN);

    if (adc >= NO_OF_SAMPLES)
    {
        adc_reading /= NO_OF_SAMPLES;
        adc_reading = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars_1);
        xdc_U = ((R3 + R4) / R4 * adc_reading);
        adc_reading = 0;
    }
}

void xdc_i()
{
    if (xdc_U == 0)
    {
        xdc_I = 0;
    }
    else
    {
        xdc_I = tyn_P / (xdc_U / 1000) * 960;
    }
}

void tyn_p()
{
    tyn_P = tyn_U * tyn_I / 1000000;
}

unsigned long preHeartTick2 = 0; // 心跳
unsigned long preHeartTick3 = 0; // 心跳
void tyn_w()
{
    if (millis() - preHeartTick2 >= 1000)
    {
        static float k = kWh;

        Q += (xdc_I / 3600000);
        kWh += (tyn_P / 3600000);
        if (kWh - k > 0.02 && millis() - preHeartTick3 >= 60000)
        {
            k = kWh;
            operateOnDataInFile("/data.txt", "Q", "W", Q);
            operateOnDataInFile("/data.txt", "kWh", "W", kWh);
            preHeartTick3 = millis();
        }
        preHeartTick2 = millis();
    }
}

void MOS_t()
{

    static int adc_reading = 0;
    adc_reading += adc1_get_raw((adc1_channel_t)MOS_PIN);

    if (adc >= NO_OF_SAMPLES)
    {
        adc_reading /= NO_OF_SAMPLES;
        adc_reading = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars_3);
        MOS_T = map(adc_reading, 715 * 2, 2350 * 2, 1200, 6100) / 100.00;
        adc_reading = 0;
    }
}
void qidongADC()
{
    adc++;
    tyn_u();
    tyn_i();
    tyn_p();
    tyn_w();
    xdc_u();
    xdc_i();
    MOS_t();
    if (adc >= NO_OF_SAMPLES)
    {
        adc = 0;
    }
}
